package org.example.myleet.p622;

public class MyCircularQueue {

    private final int maxSize;

    private int size;

    private final Node dummyHead;

    private final Node dummyTail;

    public MyCircularQueue(int k) {
        maxSize = k;
        size = 0;
        dummyHead = new Node(-1);
        dummyTail = new Node(-1);
        dummyHead.next = dummyTail;
        dummyTail.prev = dummyHead;
    }

    public boolean enQueue(int value) {
        if (size >= maxSize) {
            return false;
        }
        Node newNode = new Node(value);
        Node prev = dummyTail.prev;
        prev.next = newNode;
        newNode.prev = prev;
        newNode.next = dummyTail;
        dummyTail.prev = newNode;
        ++size;
        return true;
    }

    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        Node next = dummyHead.next.next;
        dummyHead.next = next;
        next.prev = dummyHead;
        --size;
        return true;
    }

    public int Front() {
        return dummyHead.next.v;
    }

    public int Rear() {
        return dummyTail.prev.v;
    }

    public boolean isEmpty() {
        return size <= 0;
    }

    public boolean isFull() {
        return size >= maxSize;
    }

    static class Node {
        int v;
        Node prev;
        Node next;

        public Node(int v) {
            this.v = v;
        }
    }
}
